package com.ttt.adbgui.controller;

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.view.swing.BrowserView;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;

@Slf4j
public class TestTable {

    public static void main(String[] args) {

        // testBrowser();
        runCmd("C:\\","ping www.baidu.com -t");

    }

    static void testBrowser() {
        System.setProperty("jxbrowser.license.key", "1BNDHFSC1G3706U7I8G8GUQZ2GT3MNJ43F9JR744R6ZOUZ2SDBGE39WJ1KQ0JU92X3RVR0");
        // Initialize Chromium.
        // Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
        Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).build());
        // Create a Browser instance.
        Browser browser = engine.newBrowser();

        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("JxBrowser AWT/Swing");
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    // Shutdown Chromium and release allocated resources.
                    engine.close();
                }
            });
            // Create and embed Swing BrowserView component to display web content.
            frame.add(BrowserView.newInstance(browser));
            frame.setSize(1280, 800);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);

            // Load the required web page.
            browser.navigation().loadUrl("https://html5test.com/");
        });

        // -Djxbrowser.license.key=1BNDHFSC1G3706U7I8G8GUQZ2GT3MNJ43F9JR744R6ZOUZ2SDBGE39WJ1KQ0JU92X3RVR0
    }

    static void testBaiduZiCi(){

        // 4E00-9FA5 19968 40869
        int i = Integer.valueOf("4E00", 16).intValue();
        System.out.println(i);
        char result = (char)i;
        System.out.println(result);

//        OkHttpClient client = new OkHttpClient().newBuilder()
//                .build();
//        MediaType mediaType = MediaType.parse("text/plain");
//        RequestBody body = RequestBody.create(mediaType, "");
//        Request request = new Request.Builder()
//                .url("https://hanyu.baidu.com/s?wd=<wd>&from=<from>")
//                .method("GET", body)
//                .addHeader("User-Agent", "apifox/1.0.0 (https://www.apifox.cn)")
//                .build();
//        Response response = client.newCall(request).execute();
    }

    // test cmd getString
    public static String runCmd(String cmdpath, String cmd) {

        log.info(cmd);
        String result="";
        File dir = new File(cmdpath);

        try {
            Process ps = Runtime.getRuntime().exec(cmd, null, dir);
            BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream(), Charset.forName("GBK")));
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                result+=line+"\n";
            }
            br.close();
            System.out.println("close ... ");
            ps.waitFor();
            System.out.println("wait over ...");
            return result;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("child thread donn");
        return null;
    }
}
